//
//  UIView+ZSAnimation.m
//  Epayment
//
//  Created by safiri on 2018/8/10.
//  Copyright © 2018年 safiri. All rights reserved.
//

#import "UIView+ZSAnimation.h"
#import <objc/runtime.h>
#import <pop/POP.h>

@implementation UIView (ZSAnimation)

- (void)setIsRoating:(BOOL)isRoating {
    objc_setAssociatedObject(self, @selector(isRoating), @(isRoating), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)isRoating {
    NSNumber *num = objc_getAssociatedObject(self, @selector(isRoating));
    return num.boolValue;
}
- (void)setIsScaling:(BOOL)isScaling {
    objc_setAssociatedObject(self, @selector(isScaling), @(isScaling), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)isScaling {
    NSNumber *num = objc_getAssociatedObject(self, @selector(isScaling));
    return num.boolValue;
}


- (void)rotateSelf {
    
    POPBasicAnimation *anim = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerRotation];
    anim.fromValue = @(0);
    anim.toValue = @(M_PI);
    anim.duration = 0.7;
    anim.repeatCount = 100000;
    anim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [self.layer pop_addAnimation:anim forKey:@"refreshRoat"];
    self.isRoating = YES;
}
- (void)stopRotateSelf {
    
    POPBasicAnimation *stopAnim = [POPBasicAnimation animationWithPropertyNamed:kPOPLayerRotation];
    stopAnim.toValue = @(M_PI);
    stopAnim.duration = 0.7;
    stopAnim.repeatCount = 1;
    stopAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    [stopAnim setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
        if (finished) {
            [self.layer pop_removeAllAnimations];
        }
    }];
    [self.layer pop_addAnimation:stopAnim forKey:@"stopRefreshRoat"];
    self.isRoating = NO;
}

- (void)scaleOnceSelf {
    POPSpringAnimation *scaleAnimation = [POPSpringAnimation animation];
    scaleAnimation.property = [POPAnimatableProperty propertyWithName:kPOPViewScaleXY];//宽高改变
    scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.3, 1.3)];//1.3倍
    [self pop_addAnimation:scaleAnimation forKey:@"scaleAnimationKey"];//执行动画
    scaleAnimation.completionBlock = ^(POPAnimation *animation,BOOL finish) { //动画回调
        POPSpringAnimation *scaleAnimation = [POPSpringAnimation animation];
        scaleAnimation.springBounciness = 16;    // value between 0-20
        scaleAnimation.springSpeed = 14;     // value between 0-20
        scaleAnimation.property = [POPAnimatableProperty propertyWithName:kPOPViewScaleXY];
        scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.0, 1.0)];
        [self pop_addAnimation:scaleAnimation forKey:@"scaleAnimationKey"];
    };
}

- (void)scaleSelf {
    POPBasicAnimation *scaleUpAnimation = [POPBasicAnimation animation];
    scaleUpAnimation.property = [POPAnimatableProperty propertyWithName:kPOPViewScaleXY];//宽高改变
    scaleUpAnimation.fromValue = [NSValue valueWithCGSize:CGSizeMake(1, 1)];
    scaleUpAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.3, 1.3)];//1.3倍
    scaleUpAnimation.duration = 0.7;
    
    __weak typeof(scaleUpAnimation) weakScaleUpAnimation = scaleUpAnimation;
    [weakScaleUpAnimation setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
        __strong typeof(weakScaleUpAnimation) strongScaleUpAnimation = weakScaleUpAnimation;
        if (finished) {
            POPBasicAnimation *scaleDownAnimation = [POPBasicAnimation animation];
            scaleDownAnimation.property = [POPAnimatableProperty propertyWithName:kPOPViewScaleXY];//宽高改变
            scaleDownAnimation.fromValue = [NSValue valueWithCGSize:CGSizeMake(1.3, 1.3)];
            scaleDownAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1, 1)];//1.3倍
            scaleDownAnimation.duration = 0.7;
            [scaleDownAnimation setCompletionBlock:^(POPAnimation *anim, BOOL finished) {
                if (finished && self.isScaling) {
                    [self pop_addAnimation:strongScaleUpAnimation forKey:@"scaleUpAnimationKey"];//执行动画
                }else {
                    [self pop_removeAnimationForKey:@"scaleUpAnimationKey"];
                    [self pop_removeAnimationForKey:@"scaleDownAnimationKey"];
                }
            }];
            [self pop_addAnimation:scaleDownAnimation forKey:@"scaleDownAnimationKey"];
        }
    }];
    self.isScaling = YES;
    [self pop_addAnimation:scaleUpAnimation forKey:@"scaleUpAnimationKey"];
}

- (void)stopScaleSelf {
    self.isScaling = NO;
}

- (void)rotationY3DSelf {
    CABasicAnimation *transformAnima = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
    
    transformAnima.toValue = [NSNumber numberWithFloat:M_PI];
    transformAnima.duration = 0.6;
    transformAnima.cumulative = NO;
    // 动画结束时是否执行逆动画
    transformAnima.autoreverses = YES;
    
    transformAnima.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    self.layer.zPosition = 5;
    self.layer.zPosition = self.layer.frame.size.width / 2.f;
    transformAnima.repeatCount = MAXFLOAT;
    [self.layer addAnimation:transformAnima forKey:@"rotationY3DAnimation"];
}
- (void)stopRotationY3DSelf {
    [self.layer removeAnimationForKey:@"rotationY3DAnimation"];
}
@end
